// pages/detail/detail.js
const server = require("../../server/server.js");
Page({
    data:{
        product:{},
        modes:{
            "1": "物品交换",
            "2": "现金交易",
            "3": "物品求购"
        },
        priveImgs:[]
    },
    onLoad:function(options) {
        // console.log(options)
        wx.showLoading({
            title:"数据加载中",
            mask:true
        });
        this.loadProudctDetail(options);
    },
    loadProudctDetail(params){
        server.loadProductById(params)
            .then((data)=>{
                // console.log(data)
                this.setData({
                    product:data[0]
                })
                // console.log(product)
            })
            .catch((error)=>{
                server.errorFunction(error,()=>{
                    wx.navigateBack();
                })
            })
            .finally(()=>{
                wx.hideLoading();
            })
    },
    
    showImg:function(){
        if(this.data.priveImgs.length == 0){
            let tempImgs = JSON.parse(this.data.priduct.imgurls);
            tempImgs = Object.values(tempImgs);
            tempImgs.forEach((url,i)=>{
                tempImgs[i] = "https://oa.itany.com/eximg/"+url
            });
            this.setData({
                priveImgs:tempImgs
            })
        }
        wx.previewImage({
            url: this.data.priveImgs
        });
    },

    changePage(){
        wx.navigateTo({
            url:"/pages/msg-detail/msg-detail"
        })
    }
})